package com.pojo;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.mysql.cj.result.StringValueFactory;

import com.pojo.student;
@Repository("stuDao")
public class stuDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    public int addstudent(student s) {
        String sql="insert into student(id,Name,age,livearea) values(?,?,?,?)";
        //将参数s的键值存到数组对象中
        Object[] obj=new Object[] {
                s.getId(),
                s.getName(),
                s.getAge(),
                s.getLivearea(),
        };
        //执行添加操作，update方法返回的是操作数据库后受影响的行数
        int num=this.jdbcTemplate.update(sql, obj);
        return num;

    }

    public int updateNamebySno(student s) {
        String sql="update student set Name=? where id=?";
        Object[] obj =new Object[] {
                s.getName(),
                s.getId(),
        };
        int num =this.jdbcTemplate.update(sql, obj);
        return num;

    }
    public int deletestudentbySno(String id) {
        String sql="delete from student  where id=?";
        int num =this.jdbcTemplate.update(sql, id);
        return num;

    }
    public List<student> findAllstudent(){
        String sql="select * from student";
        RowMapper<student> rowMapper=new BeanPropertyRowMapper<student>(student.class);
        return this.jdbcTemplate.query(sql, rowMapper);
    }
    public String findNameBySno(String id) {
        String sql="select Sname from student where id=?";
        String name=this.jdbcTemplate.queryForObject(sql, new Object[] {id}, String.class);
        return name;
    }
    public int findNumbers() {
        String sql="select COUNT(*) from student";
        int total =this.jdbcTemplate.queryForObject(sql, Integer.class);
        return total;
    }
    public List<Map<String, Object>> findScBySno(String id){
        String sql="select *  from student where ";
        return this.jdbcTemplate.queryForList(sql, new Object[] {id});
    }

}

